package com.example.administrator.familfinalcialmanagement.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.IndexTimeBean;
import com.example.administrator.familfinalcialmanagement.bean.PayInfoBean;
import com.example.administrator.familfinalcialmanagement.bean.ResidueBean;
import com.example.administrator.familfinalcialmanagement.bean.WalletBean;
import com.example.administrator.familfinalcialmanagement.databinding.FragmentIndexBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.PayInfoBeanDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.ResidueBeanDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.WalletBeanDao;
import com.example.administrator.familfinalcialmanagement.ui.activity.TodayPayActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.IndexTimeAdapter;
import com.example.administrator.familfinalcialmanagement.util.GlideUtil;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;
import com.example.administrator.familfinalcialmanagement.util.dialog.MyDialog;
import com.example.administrator.familfinalcialmanagement.util.listener.OnGRClickListener;
import com.example.administrator.familfinalcialmanagement.util.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/26.
 */

public class IndexFragment extends BaseLazyFragment {

    FragmentIndexBinding binding;
    List<IndexTimeBean> list = new ArrayList<>();
    PayInfoBeanDao daobeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getPayInfoBeanDao();
    ResidueBeanDao residueBeanDao = GreenDaoUtils.getSingleTon().getmDaoSession().getResidueBeanDao();
    List<PayInfoBean> listPays = new ArrayList<>();
    IndexTimeAdapter adapter;

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_index, container, false);
        binding.setFragment(this);
        return binding.getRoot();
    }

    @Override
    public int setLayoutId() {
        return R.layout.fragment_index;
    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void lazyLoad() {
        binding.llModuleResource.setOnClickListener(this);
        listPays.clear();
        listPays = daobeanDao.loadAll();
        LogUtil.e("消费记录"+ listPays.size() + "条");
        /**
         * 1.loadAll();----------查询所有的数据
         * 2.条件查询---查询单个字段 获取对象-----eq方法
         * TaskPath taskPath = mTaskPathDao.queryBuilder().where(TaskPathDao.Properties.Task_name.eq("小红帽")).unique();
         * 3.条件查询---查询不是该字段内容的 对象有哪些---notEq方法
         * List mtaskPath = mTaskPathDao.queryBuilder().where(TaskPathDao.Properties.Task_name.notEq("小黄人")).list();
         * 4.条件查询--模糊查询之包含某个特殊字的查询--queryLike方法
         *  List mtaskPath = mTaskPathDao.queryBuilder().where(TaskPathDao.Properties.Task_name.like("小%")).list();
         */
        //        List<PayInfoBean> infoBeans = daobeanDao.queryBuilder().where(PayInfoBeanDao.Properties.PayStatus.eq("支出")).list();
        //        ToastUtil.showToast("查询结果" + infoBeans.size());

        list.clear();
        list.add(new IndexTimeBean("今天", "2019-04-01", "共有" + listPays.size() + "个记账记录"));
        list.add(new IndexTimeBean("本周", "2019-04-01", "共有" + listPays.size() + "个记账记录"));
        list.add(new IndexTimeBean("本月", "2019-04-01", "共有" + listPays.size() + "个记账记录"));

        List<ResidueBean> residueBeans = residueBeanDao.loadAll();
        if (residueBeans.size() != 0) {
            //预算金额
            Double a = Double.valueOf(residueBeans.get(0).getResidueMoney());
            //收入金额
            Double b = 0.0;
            //支出金额
            Double c = 0.0;
            for(PayInfoBean payInfoBean : listPays){
                if ("收入".equals(payInfoBean.getPayStatus())) {
                    b += Double.valueOf(payInfoBean.getMoney());
                }

                if ("支出".equals(payInfoBean.getPayStatus())) {
                    c += Double.valueOf(payInfoBean.getMoney());
                }
            }
            binding.tvModuleResidue.setText((a + b - c) + "");
        }

        adapter = new IndexTimeAdapter(getActivity(), list);
        binding.recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.recycler.setAdapter(adapter);
        adapter.setOnItemClickLitener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (0 == position) {
                    startActivity(new Intent(getActivity(), TodayPayActivity.class));
                }
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    /**
     * 得到轮播图的View
     *
     * @param context
     * @param url
     * @return
     */
    public static View getImageView(Context context, String url) {
        RelativeLayout rl = new RelativeLayout(context);
        ImageView imageView = new ImageView(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(layoutParams);
        GlideUtil.setCircleMethod(context, url, imageView);
        ImageView backGround = new ImageView(context);
        backGround.setLayoutParams(layoutParams);
        backGround.setBackgroundResource(R.color.cycle_image_bg);
        rl.addView(imageView);
        rl.addView(backGround);
        return rl;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.llModuleResource:
                MyDialog dialog = new MyDialog(getActivity());
                dialog.show();
                dialog.setOnGRClickListener(new OnGRClickListener() {
                    @Override
                    public void setOnConfirmClick(String res) {
                        binding.tvModuleResource.setText(res);
                        ResidueBean bean = new ResidueBean();
                        bean.setResidueMoney(res);
                        residueBeanDao.deleteAll();
                        residueBeanDao.insert(bean);

                        //预算金额
                        Double a = Double.valueOf(binding.tvModuleResource.getText().toString().trim());
                        //收入金额
                        Double b = 0.0;
                        //支出金额
                        Double c = 0.0;
                        for(PayInfoBean payInfoBean : listPays){
                            if ("收入".equals(payInfoBean.getPayStatus())) {
                                b += Double.valueOf(payInfoBean.getMoney());
                            }

                            if ("支出".equals(payInfoBean.getPayStatus())) {
                                c += Double.valueOf(payInfoBean.getMoney());
                            }
                        }
                        binding.tvModuleResidue.setText((a + b - c) + "");
                    }

                    @Override
                    public void setOnCloseClick() {

                    }
                });
                break;
        }
    }
}
